package javachess;

public class Pawn extends Piece
{
	boolean hasMoved;
	String m_name = "Pawn";
	public Pawn(int a_owner, int a_x, int a_y)
	{
		super ('p', a_owner);
		m_x = a_x;
		m_y = a_y;
		hasMoved = false;
	}
	/**
	 * -Takes in the y destination to determine if the move is acceptable.
	 * -Returns a boolean to indicate 
	 * @param a_y
	 * @return
	 */
	public boolean validateMove(Player player, Player otherPlayer, int a_x, int a_y)
	{
		
		for(int i = 0; i < player.m_pieces.size(); ++i)
		{
			if(player.m_pieces.get(i).m_x == a_x 
			&& player.m_pieces.get(i).m_y == a_y)
			{
				return false;
			}
		}
		
		// PAWN MOVER MULTIPLIER (FOR DIFFERENT PLAYERS
		int pawnMovementMultiplier;
		if (this.m_owner == 0)
			pawnMovementMultiplier = 1; // THIS MOVES THE PAWN UP
		else
			pawnMovementMultiplier = -1; // THIS MOVES THE PAWN DOWN

		if ((a_x == m_x) 
		&& (a_y == m_y + (1 * pawnMovementMultiplier)))
		{
			for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
			{
				if(otherPlayer.m_pieces.get(i).m_x == a_x 
				&& otherPlayer.m_pieces.get(i).m_y == a_y)
				{
					return false;
				}
			}
			hasMoved = true;
			return true;
		}
		
		if(!this.hasMoved)
		{
			if ((a_x == m_x) 
			&& (a_y == m_y + (2 * pawnMovementMultiplier)))
			{
				for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
				{
					if(otherPlayer.m_pieces.get(i).m_x == a_x 
					&& otherPlayer.m_pieces.get(i).m_y == a_y)
					{
						return false;
					}
				}	
				hasMoved = true;
				return true;
			}
		}
		
		if ((a_x == (m_x+1) || a_x == (m_x-1))
				&& (a_y == m_y + (1 * pawnMovementMultiplier)))
		{
			for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
			{
				if(otherPlayer.m_pieces.get(i).m_x == a_x 
				&& otherPlayer.m_pieces.get(i).m_y == a_y)
				{
					hasMoved = true;
					return true;
				}
			}	
		}
		
		return false;
		
		/*
		//these must remain in this scope in order to be reset if a swap occurred
		int temp_y = m_y;
		int temp_x = m_x;
		//if the destination y is less than the object y, swap them
		if(a_y < m_y)
		{
			m_y = a_y;
			a_y = temp_y;
		}
		//if the destination x is less than the object x, swap them
		if(a_x < m_x)
		{
			m_x = a_x;
			a_x = temp_x;
		}
		//if the pawn has not made its first move of the game
		if((a_y - m_y) < 3 && (a_y - m_y) > 0 && hasMoved == false)
		{
			//in case a swap happened
			m_x = temp_x;
			m_y = temp_y;
			for(int i = 0; i < player.m_pieces.size(); ++i)
			{
				//if the location is equal to one of the player's own pieces
				if(player.m_pieces.get(i).m_x == a_x && player.m_pieces.get(i).m_y == a_y )
					return false;
			}
			for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
			{
				//if the location is equal to another player's piece
				if(otherPlayer.m_pieces.get(i).m_x == a_x && otherPlayer.m_pieces.get(i).m_y == a_y )
					return false;
			}
			hasMoved = true;
			return true;
		}
		//if the pawn has made its first move of the game
		else if((a_y - m_y) == 1 && hasMoved == false)
		{
			//in case a swap happened
			m_x = temp_x;
			m_y = temp_y;
			for(int i = 0; i < player.m_pieces.size(); ++i)
			{
				//if the location is equal to one of the player's own pieces
				if(player.m_pieces.get(i).m_x == a_x && player.m_pieces.get(i).m_y == a_y )
					return false;
			}
			for(int i = 0; i < otherPlayer.m_pieces.size(); ++i)
			{
				//if the location is equal to another player's piece
				if(otherPlayer.m_pieces.get(i).m_x == a_x && otherPlayer.m_pieces.get(i).m_y == a_y )
					return false;
			}
			hasMoved = true;
			return true;
		}
		//if the pawn was moved diagonally
		else if((a_y - m_y == 1) && (a_x - m_x == 1))
		{
			for(int i = 0; i < player.m_pieces.size(); i ++)
			{
				//if the location is equal to another player's piece
				if(otherPlayer.m_pieces.get(i).m_x == a_x && otherPlayer.m_pieces.get(i).m_y == a_y)
				{
					return true;
				}
			}
			return false;	
		}
		m_x = temp_x;
		m_y = temp_y;
		return false;
		*/
	}
}
